package com.laikan.reader.bookreader.engines;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.*;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import com.laikan.library.utils.AndroidUtils;
import com.laikan.library.utils.SPUtil;
import com.laikan.reader.R;
import com.laikan.reader.core.utils.PublicUtil;
import com.socks.library.KLog;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by wx
 *
 * @date 2016/5/26/10:32
 * @description 书籍加工类
 */

public class PageFactory {

    /***
     * 右标点符号，不能再开头的符合列表
     */
    public static final char[] specialCharRight = {10, 32, 34, 33, 41, 44, 46, 58, 59, 62, 93, 125, 183, 8217, 8221, 8758, 12288, 12289, 12290, 12297, 12299, 12301, 12303, 12305, 12309, 12311, 65281, 65282, 65289, 65292, 65294, 65306, 65307, 65310, 65311, 65341, 65373};
    /***
     * 左标点符号
     */
    public static final char[] specialCharLeft = {40, 60, 91, 123, 160, 8216, 8220, 12296, 12298, 12300, 12302, 12304, 12308, 12310, 65288, 65308, 65339, 65371};
    public static final char[] specialCharLine = {126, 8212, 8230, 8943, 65374};
    public final static String TAG = "BookPageFactory";
    static HashSet<String> mapRight = new HashSet<String>();
    static HashSet<String> mapLeft = new HashSet<String>();
    /***
     * 标点符号
     */
    static HashSet<String> mapMark = new HashSet<String>();

    /***
     * 时间
     */
    public Context mContext;
    /***
     * 是否通知相关事件
     */
    public boolean IsNotify = false;
    /***
     * 可视区域宽度
     */
    private int mWidth;
    /***
     * 可视区域高度
     */
    private int mHeight;
    private List<Vector<WordLinePos>> pages;
    private PageStyleSet pss;
    private OnCompleteData onCompleteData;
    Bitmap battery_bg = null;
    String dateTime;
    private String bookName;

    /***
     * 初始化
     */
    public PageFactory(PageStyleSet pss, Context context, String bookName) {
        this.pss = pss;
        this.bookName = "《" + bookName + "》";
        for (char ch : specialCharRight) {
            mapRight.add(ch + "");
            mapMark.add(ch + "");
        }
        for (char ch : specialCharLeft) {
            mapLeft.add(ch + "");
            mapMark.add(ch + "");
        }
        mWidth = PageStyleSet.screenWidth;
        mHeight = PageStyleSet.screenHeight;
        this.mContext = context;
    }

    /***
     * 分页
     */
    public synchronized void setbookData(ChapterStatus chapter, int status, boolean isReload) {
        if (chapter != null) {
            chapter.setReload(isReload);
        }
        String content = chapter.getChapterCmd();
        pages = new ArrayList<>();
        if (content == null || content.equals("")) {
            KLog.v(TAG, "内容为空");
            return;
        }
        Paint defPaint = new Paint();
        Paint conPaint = pss.GetContentPaint();
        /** 绘制内容宽度 */
        float mVisibleWidth = mWidth - pss.GetMarginWidth();
        /** 绘制内容的高度 */
        float mVisibleHeight = mHeight - pss.GetMarginHeight();

        char[] arr = content.toCharArray();
        /** content的遍历下标 */
        int pos = 0;
        /** 若干行。 一页有若干行组成 */
        Vector<WordLinePos> lines = new Vector<WordLinePos>();
        /** 当前绘制到的高度 */
        float y = 0;
        boolean isZzsFrist = false;
        /** 一个段落 */
        StringBuilder paragraph = new StringBuilder(200);
        for (char ch : arr) {
            if (ch != '\n') {
                paragraph.append(ch);
                continue;
            }
            String p = paragraph.toString();
            paragraph.setLength(0);
            pos += p.toCharArray().length;
            p = p.trim();
            if (p.equals("")) {
                continue;
            }

            if (p.startsWith(CmdBuild.BTag + CmdType.font)) {// 特殊字体
                char[] parr = CmdBuild.getContent(p);
                int pi = 0;
                float fontSize = defPaint.getTextSize();// 一个字的宽、高
                float halfFontSize = fontSize / 2;
                float lineSpaceing = pss.lineSpaceingRatio * fontSize;
                float paragraphSpacing = fontSize * pss.paragraphSpacingRatio;
                float paragraphIndent = fontSize * pss.paragraphIndentRatio;
                StringBuilder line = new StringBuilder(30);
                float lw = 0; // 当前行宽
                float fw = 0; // 当前字宽
                boolean pStartIndent = true;// 段落第一行就需要放到下一页时，段首缩需要进行缩进
                int textType = 1;// 字体类型 0正文字体，1特殊字体，2标题，3作者说

                if (p.startsWith(CmdBuild.BTag + CmdType.font + CmdBuild.STag + "title"))// 标题
                {

                    textType = 2;
                    pStartIndent = false;
                    lineSpaceing = pss.lineSpaceingRatio * conPaint.getTextSize();// 普通行距
                    WordLinePos word = new WordLinePos(CmdBuild.BTag + CmdType.blank);// 加入空白段落
                    word.top = y + pss.marginTop;
                    word.bottom = word.top + pss.distanceTop;
                    word.right = mWidth - pss.marginHorizontal;
                    word.left = pss.marginHorizontal;
                    lines.add(word);
                    y += pss.distanceTop;
                    defPaint.setTextSize(pss.GetContentPaint().getTextSize() + 3 * PageStyleSet.density);
                    fontSize = defPaint.getTextSize();// 一个字的宽、高
                    halfFontSize = fontSize / 2;
                    lineSpaceing = pss.lineSpaceingRatio * fontSize;
                    paragraphSpacing = fontSize * pss.paragraphSpacingRatio;
                    paragraphIndent = fontSize * pss.paragraphIndentRatio;
                }else if (p.startsWith(CmdBuild.BTag + CmdType.font + CmdBuild.STag + "zzs"))// 作者说距离上部距离
                {
                    textType = 3;

                    if (!isZzsFrist) {
                        isZzsFrist = true;
                        WordLinePos word = new WordLinePos(CmdBuild.BTag + CmdType.blank);// 加入空白段落
                        word.top = y + pss.marginTop;
                        word.bottom = word.top + pss.distanceTop;
                        word.right = mWidth - pss.marginHorizontal;
                        word.left = pss.marginHorizontal;
                        lines.add(word);

                        y += pss.zzsDistanceTop;
                    }

                    defPaint.setTextSize(pss.GetContentPaint().getTextSize() - 1 * PageStyleSet.density);
                    fontSize = defPaint.getTextSize();// 一个字的宽、高
                    halfFontSize = fontSize / 2;
                    lineSpaceing = pss.lineSpaceingRatio * fontSize;
                    paragraphSpacing = fontSize * pss.paragraphSpacingRatio;
                    paragraphIndent = fontSize * pss.paragraphIndentRatio;
                }else if (y != 0) {
                    y += paragraphSpacing;// 段间距,新页且开始新的段落 不加段间距//!!!标题与正文有段间距
                }

                while (pi < parr.length) {// 对段进行分行计算
                    if (y + fontSize > mVisibleHeight) {
                        pages.add(lines);
                        lines = new Vector<WordLinePos>();
                        y = 0;
                    }

                    lw = pStartIndent ? paragraphIndent : 0;
                    fw = defPaint.measureText(parr[pi] + "");
                    line.setLength(0);

                    for (; lw + fw < mVisibleWidth; ) {// 求行
                        line.append(parr[pi]);

                        lw += (mapMark.contains(parr[pi] + "") ? 0 : pss.wordSpacingRatio * fw) + fw;

                        if (++pi > parr.length - 1) {
                            break;
                        }
                        fw = defPaint.measureText(parr[pi] + "");
                    }

                    if (pi < parr.length && mapRight.contains(parr[pi] + "")) {// 处理标点符号。绘制时，标点向上压缩间距。
                        line.append(parr[pi++]);
                        if (pi < parr.length && mapRight.contains(parr[pi] + "")) {
                            line.append(parr[pi++]);
                        }
                    }

                    if (line.length() > 0) {
                        WordLinePos word = new WordLinePos(CmdBuild.getCommand(line.toString(), p));
                        ((CmdText)word.cmd).textType = textType;

                        word.top = y + pss.marginTop;
                        word.bottom = word.top + fontSize;
                        word.right = mWidth - pss.marginHorizontal;
                        word.left = (pStartIndent ? paragraphIndent : 0) + pss.marginHorizontal;

                        lines.add(word);
                        pStartIndent = false;
                        y += fontSize + lineSpaceing;
                    }
                }// end 段落

                if (lines.size() > 0) {
                    WordLinePos word = lines.get(lines.size() - 1);
                    word.right = lw + pss.marginHorizontal;
                }
            }else if (p.startsWith(CmdBuild.BTag + CmdType.img)) {// 图片
                if (y + pss.imageHeight > mVisibleHeight) {// 如果该页面放不下该图片，放到下一页
                    pages.add(lines);
                    lines = new Vector<WordLinePos>();
                    y += pss.imageHeight;

                    int index = p.lastIndexOf(CmdBuild.STag);
                    String url = p.substring(index + CmdBuild.STag.length(), p.length() - CmdBuild.ETag.length());
                }
                // 能放下放在该页面
                // lines.add(CreateTextWordLine(p));
            }else if (p.startsWith(CmdBuild.BTag + CmdType.line)) {// 行线条

                WordLinePos word = new WordLinePos(p);
                word.top = y + pss.marginTop;
                word.bottom = word.top + pss.contentPaint.getTextSize();
                word.left = pss.marginHorizontal;
                word.right = mWidth - pss.marginHorizontal;

                y += pss.contentPaint.getTextSize();
                lines.add(word);
                continue;
            }else if (p.startsWith(CmdBuild.BTag + CmdType.interaction)) {// 自定义互动区域
                float interactionHeight = 68f / 1280f * mHeight;
                if (y + 300 > mVisibleHeight) {
                    pages.add(lines);
                    lines = new Vector<WordLinePos>();
                    y = 0;
                }

                lines.add(new WordLinePos(p));// !!!处理分页
                y += interactionHeight;
                continue;
            }else if (p.startsWith(CmdBuild.BTag + CmdType.needpay)) {// 付费设置
                //                chapter.state.needPay = true;
                //                Command.SetBuyChpter(p, chapter);
                continue;
            }else {// 普通段落
                char[] parr = p.toCharArray();
                int pi = 0;
                float fontSize = conPaint.getTextSize();// 一个字的宽、高
                float halfFontSize = fontSize / 2;
                float lineSpaceing = pss.lineSpaceingRatio * fontSize;
                float paragraphSpacing = fontSize * pss.paragraphSpacingRatio;
                float paragraphIndent = fontSize * pss.paragraphIndentRatio;
                StringBuilder line = new StringBuilder(30);
                float lw = 0; // 当前行宽
                float fw = 0; // 当前字宽
                boolean isChangeLine = false;
                boolean pStartIndent = true;// 段落第一行就需要放到下一页时，段首缩需要进行缩进

                if (y != 0) {
                    y += paragraphSpacing;// 段间距,新页且开始新的段落 不加段间距//!!!标题与正文有段间距
                }

                while (pi < parr.length) {// 对段进行分行计算
                    if (y + fontSize > mVisibleHeight) {
                        pages.add(lines);
                        lines = new Vector<WordLinePos>();
                        y = 0;
                    }
                    lw = (pStartIndent ? paragraphIndent : 0);
                    fw = conPaint.measureText(parr[pi] + "");
                    line.setLength(0);

                    for (; lw + fw < mVisibleWidth; ) {// 求行
                        line.append(parr[pi]);

                        lw += (mapMark.contains(parr[pi] + "") ? 0 : pss.wordSpacingRatio * fw) + fw;

                        if (++pi > parr.length - 1) {
                            break;
                        }
                        // fw = CharBCUtil.IsDBC(parr[pi]) ? halfFontSize :
                        // fontSize;
                        fw = conPaint.measureText(parr[pi] + "");
                    }

                    if (pi < parr.length && mapRight.contains(parr[pi] + "")) {// 处理标点符号。绘制时，标点向上压缩间距。
                        line.append(parr[pi++]);
                        if (pi < parr.length && mapRight.contains(parr[pi] + "")) {
                            line.deleteCharAt(line.length() - 1);
                            line.deleteCharAt(line.length() - 2);
                            isChangeLine = true;
                            pi--;
                            pi--;
                        }
                    }
                    if (line.length() > 0) {
                        WordLinePos word = new WordLinePos(line.toString());
                        word.top = y + pss.marginTop;
                        word.bottom = y + pss.marginTop + fontSize;

                        word.right = mWidth - pss.marginHorizontal;
                        word.left = (pStartIndent ? paragraphIndent : 0) + pss.marginHorizontal;

                        lines.add(word);
                        pStartIndent = false;
                        y += fontSize + lineSpaceing;
                    }
                }// end 段落

                if (lines.size() > 0) {
                    WordLinePos word = lines.get(lines.size() - 1);
                    word.right = lw + pss.marginHorizontal;
                }
            }
        }

        if (lines.size() != 0)// 最后没够一页内容
        {
            pages.add(lines);
        }
        int i = 0;
        /** 垂直方向分散对齐 */
        for (Vector<WordLinePos> page : pages) {
            if (page.size() > 1 && pages.size() != ++i /* && i != 1 */) {// 当前行数至少2行，且不为最后一页，进行分散对齐
                float filled = page.get(page.size() - 1).bottom - page.get(0).top;// 已经填充
                float extraSpace = (mVisibleHeight - filled) / (page.size() - 1);// 将多余的空间，分配到每行

                int li = 0;// 行下标
                for (WordLinePos ll : page) {
                    ll.MoveYTo(extraSpace * li);
                    li++;
                }
            }
        }
        chapter.setReload(false);
        chapter.setChapterList(pages);
        chapter.setCompData(true);
        if (onCompleteData != null) {
            if (isReload) {
                onCompleteData.reloadPage(chapter, status);
            }else {

                onCompleteData.complete(chapter, status);
            }
        }

    }

    /***
     * 绘制
     */
    public boolean draw(Canvas c, ChapterStatus chapter, int index) {

        if (chapter.getChapterList().size() == 0) {// 后一章节数据为空
            return false;
        }
        if (index > chapter.getChapterList().size() - 1) {
            return false;
        }
        Vector<WordLinePos> lines = chapter.getChapterList().get(index);
        if (lines.size() == 0) {
            return true;
        }

        for (WordLinePos line : lines) {

            if (line.cmd instanceof CmdText) { // 字体
                CmdText cmdText = (CmdText)line.cmd;

                Paint p = new Paint();
                if (cmdText.textType == 1) {// 特殊字体
                }else if (cmdText.textType == 2) {// 标题
                    p = pss.GetTitlePaint();

                    String text = cmdText.GetText();
                    c.drawText(text, line.left, line.bottom, p);
                    continue;
                }else if (cmdText.textType == 3) {// 作者说
                    Paint pzzs = new Paint();
                    pzzs.setTextSize(pss.GetContentPaint().getTextSize() - 1 * PageStyleSet.density);
                    DrawOneLineText(c, line, pzzs);
                }else {
                    p = pss.GetContentPaint();// 正文字体
                    //                      Command.setContentPaintColor(pss);
                    DrawOneLineText(c, line, p);
                }

            }else if (line.cmd instanceof CmdLine) {// 行线条
                Paint p = pss.GetLinePaint();// !!!完善线条样式
                p.setStrokeWidth(2);
                float y = (line.top + 6 * PageStyleSet.density);
                c.drawLine(line.left, y, line.right, y, p);
                continue;
            }else if (line.cmd instanceof CmdImg) {// 图片类型

            }
        }

        String nameChapter = chapter.getChapterName();
        Paint statePaint = pss.GetStatePaint();
        //        if (index != 0) { // 绘制左上角章节标题  是否是第一页
        /** 左上角章节标题y的位置 */
        Rect nameRect = new Rect();
        Rect chapterRect = new Rect();
        Rect recFont = new Rect();
        Rect chapterFontRect = new Rect();
        int vWidth = mWidth - pss.GetMarginWidth();
        float startX = pss.marginHorizontal;
        float statusY = mHeight * (20f / 1136f) + statePaint.getTextSize();
        statePaint.getTextBounds(bookName, 0, bookName.length(), nameRect);
        if ((startX + nameRect.width()) > vWidth) {    // 如果左上角小标题超出可视区域,则将标题截取,并在末尾处添加"..."
            for (int i = 0; i < bookName.length(); i++) {
                statePaint.getTextBounds(bookName, 0, i, recFont);
                if ((startX + recFont.width()) > vWidth - statePaint.getTextSize()) {
                    c.drawText(bookName.substring(0, i) + "...", startX, statusY, statePaint);
                    break;
                }
            }
        }else {    // 如果左上角小标题没有超出可视区域,则直接绘制
            c.drawText(bookName, startX, statusY, statePaint);
            statePaint.getTextBounds(nameChapter, 0, nameChapter.length(), chapterRect);
            float chaperSpace = vWidth - nameRect.width();
            if (chapterRect.width() > chaperSpace) {

                for (int j = 0; j < nameChapter.length(); j++) {
                    statePaint.getTextBounds(nameChapter, 0, j, chapterFontRect);
                    if (chapterFontRect.width() > chaperSpace - AndroidUtils.dip2px(mContext, 15) - statePaint.getTextSize()) {
                        c.drawText(nameChapter.substring(0, j) + "...", startX + nameRect.width() + AndroidUtils.dip2px(mContext, 15), statusY, statePaint);
                        break;
                    }
                }
            }else {
                c.drawText(nameChapter, startX + nameRect.width() + (chaperSpace - chapterRect.width()), statusY, statePaint);
            }
        }

        //        }
        /**
         * 绘制章节末几个按钮
         */
        if (index == chapter.getChapterList().size() - 1) {
            interactionC(c, PageStyleSet.screenWidth, PageStyleSet.screenHeight);
            return true;
        }

        CalcBatteryAndTime();
        boolean IsStaticShow = true;
        if (IsStaticShow) {
            int wordwidth = (int)statePaint.measureText(dateTime);
            int pageCount = chapter.getChapterList().size();
            // 获取页码的宽度
            wordwidth = (int)statePaint.measureText(index + "/" + pageCount);
            Rect rect = new Rect();
            statePaint.getTextBounds(dateTime, 0, dateTime.length(), rect);
            float w = 1.0f;
            float bottomPadding = mHeight * 0.0176f;
            float left = pss.marginHorizontal;
            float top = mHeight - bottomPadding - rect.height();

            // 绘制页码
            float pageNumLeft = mWidth - wordwidth - pss.marginHorizontal;
            float pageNumTop = mHeight - bottomPadding;

            if (pageCount < index) {
                c.drawText(index + 1 + "/" + "..", pageNumLeft, pageNumTop, statePaint);
            }else {
                c.drawText(index + 1 + "/" + pageCount, pageNumLeft, pageNumTop, statePaint);
            }

            // 绘制电量
            if (battery_bg != null) {
                w = (battery_bg.getWidth() * (rect.height() + 2 * PageStyleSet.density)) / (float)battery_bg.getHeight();
                Bitmap b = PublicUtil.resizeBitmap(battery_bg, (int)w, (int)(rect.height() + 2 * PageStyleSet.density));
                b = pss.setBatteryColor(b);
                c.drawBitmap(b, left, top, null);
            }

            if (dateTime != null) {
                float dtLeft = left + w + mWidth * 0.01875f;
                float dtTop = mHeight - bottomPadding;
                c.drawText(dateTime, dtLeft, dtTop, statePaint);
            }
        }

        return true;
    }

    IntentFilter batteryLevelFilter;

    /**
     * 计算当前系统时间
     */
    private void CalcBatteryAndTime() {

        // 阅读页显示时间
        SimpleDateFormat sf = new SimpleDateFormat("HH:mm");
        dateTime = sf.format(new Date(System.currentTimeMillis()));
        batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        BroadcastReceiver broad = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {}
        };
        Intent bsttrty = mContext.registerReceiver(broad, batteryLevelFilter);

        if (bsttrty != null) {
            // 判断它是否是为电量变化的Broadcast Action
            // 获取当前电量
            int level = bsttrty.getIntExtra("level", 0);
            // 电量的总刻度
            int scale = bsttrty.getIntExtra("scale", 100);
            // 把它转成百分比
            int percent = level * 100 / scale;
            if (percent > 0 && percent <= 20) {
                battery_bg = PublicUtil.readBitMap(mContext, R.drawable.lk_reader_power0);
            }else if (percent > 20 && percent <= 40) {
                battery_bg = PublicUtil.readBitMap(mContext, R.drawable.lk_reader_power25);
            }else if (percent > 40 && percent <= 60) {
                battery_bg = PublicUtil.readBitMap(mContext, R.drawable.lk_reader_power50);
            }else if (percent > 60 && percent <= 80) {
                battery_bg = PublicUtil.readBitMap(mContext, R.drawable.lk_reader_power75);
            }else if (percent > 80 && percent <= 100) {
                battery_bg = PublicUtil.readBitMap(mContext, R.drawable.lk_reader_power100);
            }

        }
        mContext.unregisterReceiver(broad);
    }

    /***
     * 绘制正文字体
     */
    private void DrawOneLineText(Canvas c, WordLinePos line, Paint p) {

        String text = ((CmdText)line.cmd).GetText();
        int n = text.length();
        if (n > 1) {
            /** 一行字体需要总宽度（不包括自己间距） */
            int puch = 0;// 标点符号的个数
            for (char ch : text.toCharArray()) {
                if (mapMark.contains(ch + "")) {
                    ++puch;
                }
            }
            float mFontWidth = p.measureText(text);
            float extraSpace = (line.getWidth() - mFontWidth) / (n - 1);

            if (puch > 0 && n - puch - 1 > 0)// 标点符号不加字间距
            {
                extraSpace = (line.getWidth() - mFontWidth) / (n - puch - 1);
            }

            float x = line.left;
            boolean isLPuch = true;// 是否是左标点符号
            for (char ch : text.toCharArray()) {
                if (!isLPuch)// 左标点符号后面不加字间距
                {
                    x += (mapRight.contains(ch + "") ? 0 : extraSpace);// 右标点符号前面不加字间距
                }

                c.drawText(ch + "", x, line.bottom, p);
                // x += extraSpace + (CharBCUtil.IsDBC(ch) ? fontSize / 2 :
                // fontSize);
                x += p.measureText(ch + "");
                isLPuch = mapLeft.contains(ch + "");
            }
        }else {
            c.drawText(text, line.left, line.bottom, p);// !!!
        }
    }

    /**
     * 互动区域
     *
     * @param c
     * @param mWidth
     * @param mHeight
     *
     * @return
     */
    public Canvas interactionC(Canvas c, int mWidth, int mHeight) {

        float iocnMarTop = 1182f / 1280f * mHeight;
        float iconHeight = 32f / 1280f * mHeight;
        float textMarTop = 1230f / 1280f * mHeight;
        float lineMarTop = 1191f / 1280f * mHeight;

        float textSize = 28f / 1280f * mHeight;
        Bitmap verso_reward, verso_comment, verso_share;

        /** 打赏 */
        verso_reward = setBitmapColor(pss, Bitmap.createScaledBitmap(PublicUtil.readBitMap(mContext, R.drawable.c_reward_w), (int)iconHeight, (int)iconHeight, true));
        /** 评论 */
        verso_comment = setBitmapColor(pss, Bitmap.createScaledBitmap(PublicUtil.readBitMap(mContext, R.drawable.c_comment_w), (int)iconHeight, (int)iconHeight, true));
        /** 分享 */
        verso_share = setBitmapColor(pss, Bitmap.createScaledBitmap(PublicUtil.readBitMap(mContext, R.drawable.icon_share_e), (int)iconHeight, (int)iconHeight, true));

        /** 打赏 */

        setBitmapColor(pss, verso_reward);

        /** 评论 */

        setBitmapColor(pss, verso_comment);
        /** 分享 */

        setBitmapColor(pss, verso_share);

        float iconStart = (mWidth / 3 - verso_reward.getWidth()) / 2;
        c.drawBitmap(verso_reward, iconStart, iocnMarTop, null);
        c.drawBitmap(verso_comment, iconStart + mWidth / 3, iocnMarTop, null);
        c.drawBitmap(verso_share, iconStart + mWidth / 3 * 2, iocnMarTop, null);

        TextPaint text = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        text.setTextSize(textSize);
        text.setColor(Color.parseColor("#999999"));
        setPaintColor(text);
        // Command.setNightPaint(pss, text2, 5);
        StaticLayout static_reward = new StaticLayout("打赏", text, mWidth / 3, Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
        StaticLayout static_comment = new StaticLayout("评论", text, mWidth / 3, Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
        StaticLayout static_share = new StaticLayout("享读", text, mWidth / 3, Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);

        c.save();
        c.translate(0, textMarTop);
        static_reward.draw(c);
        c.restore();

        c.save();
        c.translate(mWidth / 3 * 1, textMarTop);
        static_comment.draw(c);
        c.restore();

        c.save();
        c.translate(mWidth / 3 * 2, textMarTop);
        static_share.draw(c);
        c.restore();
        Paint p = new Paint();
        p.setColor(Color.parseColor("#999999"));
        setPaintColor(p);
        c.drawLine(mWidth / 3 - 0.5f, lineMarTop, mWidth / 3 + 0.5f, textMarTop + text.getTextSize(), p);
        c.drawLine(mWidth / 3 * 1 - 0.5f, lineMarTop, mWidth / 3 * 1 + 0.5f, textMarTop + text.getTextSize(), p);
        c.drawLine(mWidth / 3 * 2 - 0.5f, lineMarTop, mWidth / 3 * 2 + 0.5f, textMarTop + text.getTextSize(), p);

        return null;

    }

    /**
     * 根据背景模式渲染图片颜色
     *
     * @param css
     * @param mBitmap
     *
     * @return
     */
    public Bitmap setBitmapColor(PageStyleSet css, Bitmap mBitmap) {
        Bitmap bitmap = null;
        if (css.isNighttime) {    // 夜间模式
            bitmap = PublicUtil.getAlphaBitmap(mBitmap, css.NightColor);

        }else {    // 日间模式
            int bg_model = SPUtil.getInt("bg_model", 0);
            switch (bg_model) {
                case 0:
                case 2:
                case 4:
                case 6:
                    bitmap = PublicUtil.getAlphaBitmap(mBitmap, css.DayColor);
                    break;

                case 8:
                    int color8 = mContext.getResources().getColor(R.color.C_1D1D1D);
                    bitmap = PublicUtil.getAlphaBitmap(mBitmap, color8);
                    break;

                case 10:
                    int color10 = mContext.getResources().getColor(R.color.C_777777);
                    bitmap = PublicUtil.getAlphaBitmap(mBitmap, color10);
                    break;

                case 12:
                    int color12 = mContext.getResources().getColor(R.color.C_EFEFEF);
                    bitmap = PublicUtil.getAlphaBitmap(mBitmap, color12);
                    break;

                case 14:
                    int color14 = mContext.getResources().getColor(R.color.C_677E94);
                    bitmap = PublicUtil.getAlphaBitmap(mBitmap, color14);
                    break;

                case 16:
                    int color16 = mContext.getResources().getColor(R.color.C_72634F);
                    bitmap = PublicUtil.getAlphaBitmap(mBitmap, color16);
                    break;

                case 18:
                    int color18 = mContext.getResources().getColor(R.color.C_86B4B2);
                    bitmap = PublicUtil.getAlphaBitmap(mBitmap, color18);
                    break;
            }
        }
        return bitmap;
    }

    public void setPaintColor(Paint p) {
        if (pss.isNighttime) {    // 夜间模式
            // p.setColor(pss.NightColor);
            p.setColor(mContext.getResources().getColor(R.color.C_CCCCCC));

        }else {    // 日间模式
            int bg_model = SPUtil.getInt("bg_model", 0);
            switch (bg_model) {
                case 0:
                case 2:
                case 4:
                case 6:
                    p.setColor(pss.DayColor);
                    break;

                case 8:
                    int color8 = mContext.getResources().getColor(R.color.C_1D1D1D);
                    p.setColor(color8);
                    break;

                case 10:
                    int color10 = mContext.getResources().getColor(R.color.C_777777);
                    p.setColor(color10);
                    break;

                case 12:
                    int color12 = mContext.getResources().getColor(R.color.C_EFEFEF);
                    p.setColor(color12);
                    break;

                case 14:
                    int color14 = mContext.getResources().getColor(R.color.C_677E94);
                    p.setColor(color14);
                    break;

                case 16:
                    int color16 = mContext.getResources().getColor(R.color.C_72634F);
                    p.setColor(color16);
                    break;

                case 18:
                    int color18 = mContext.getResources().getColor(R.color.C_86B4B2);
                    p.setColor(color18);
                    break;
            }
        }
    }

    /**
     * 数据记载完成
     */
    public interface OnCompleteData {

        void complete(ChapterStatus chapter, int staus);

        void reloadPage(ChapterStatus chapter, int staus);
    }

    public void setOnCompleteData(OnCompleteData l) {
        this.onCompleteData = l;
    }

}
